<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>添加用户</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
    <link rel="stylesheet" href="/static/back/css/font.css">
    <link rel="stylesheet" href="/static/back/css/xadmin.css">
    <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
    <script type="text/javascript" src="/static/back/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="/static/common/js/xadmin.js"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
      <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
      <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>

  <body>
    <div class="x-body">
        <form class="layui-form"  onsubmit="return false">
          <div class="layui-form-item">
              <label for="goodname" class="layui-form-label">
                  <span class="x-red">*</span>商品类型
              </label>
              <div class="layui-input-inline">
                  <input type="text" id="goodname" name="goodname" required="" lay-verify="required"
                  autocomplete="off" class="layui-input">
              </div>
              <div class="layui-form-mid layui-word-aux">
                  <span class="x-red">*</span>商品分类是唯一的
              </div>
          </div>
            <div class="layui-form-item">
                <label class="layui-form-label">商品图片</label>
                <div class="layui-input-inline">
                    <input type="text" name="imgUrl" lay-verify="required" id="inputimgurl" placeholder="图片地址" value="/static/back/images/404.jpg" class="layui-input">
                </div>
                <div class="layui-input-inline">
                    <div class="layui-upload-list" style="margin:0">
                        <img height="150" width="150" src="/static/back/images/404.jpg" id="srcimgurl" class="layui-upload-img">
                    </div>
                </div>
                <div class="layui-input-inline layui-btn-container" style="width: auto;">
                    <button type="button" class="layui-btn layui-btn-primary" id="editimg">修改图片</button>
                </div>
                <div class="layui-form-mid layui-word-aux">图片大小在50kb以内</div>
            </div>
          <div class="layui-form-item">
              </label>
              <button  class="layui-btn" lay-filter="add" lay-submit="">
                  增加
              </button>
          </div>
        </form>
    </div>
    <script>
        layui.use(['form', 'layer'],
            function () {
                $ = layui.jquery;
                var form = layui.form,
                    layer = layui.layer;

                layui.config({
                    base: '/static/common/js/' //layui自定义layui组件目录
                }).use(['form','croppers'], function () {
                    var   croppers = layui.croppers;
                    //创建一个上传图片组件
                    croppers.render({
                        elem: '#editimg'
                        ,saveW:200     //保存宽度
                        ,saveH:200
                        ,mark:1/1    //选取比例
                        ,area:'800px'  //弹窗宽度
                        ,url: "/admin/fileUpload"  //图片上传接口返回和（layui 的upload 模块）返回的JOSN一样
                        ,done: function(src){ //上传完毕回调
                            console.log(src);
                            $("#inputimgurl").val(src);
                            $("#srcimgurl").attr('src',src);
                        }
                    });
                });

                //监听提交
                form.on('submit(add)',
                    function (data) {
                        console.log(data);
                        //发异步，把数据提交给Control
                        $.ajax({
                            url: "save",
                            method: "post",
                            dataType: "json",
                            data: data.field,
                            success: function (result) {
                                console.log(result);
                                if (result.code === 200) {
                                    layer.alert("添加成功", {
                                            icon: 6
                                        },
                                        function () {
                                            //关闭当前frame
                                            xadmin.close();
                                            // 可以对父窗口进行刷新
                                            xadmin.father_reload();
                                        });
                                } else {
                                    layer.alert(result.msg)
                                }
                            },
                            error: function () {
                                layer.alert("程序出错")
                            }
                        });
                        return false;
                    });
            });
    </script>
  </body>

</html>
